from machine import I2C,Pin,RTC
import time,ntptime,ds18x20,onewire
import tm1650

i2c = I2C(scl=Pin(4),sda=Pin(5),freq=100000)
tm = tm1650.TM1650(i2c)


def temperature():
    ow = onewire.OneWire(Pin(14))
    ds = ds18x20.DS18X20(ow)
    roms = ds.scan()
    ds.convert_temp()
    time.sleep_ms(750)
    for rom in roms:  
        tm.temperature(ds.read_temp(rom))
    
def  irq_temperature(vb):
    time.sleep_ms(200)
    if  irq1.value() == 0 :
        print('fghj')
        temperature()
    while irq1.value() == 0:
        pass
           
irq1 = Pin(12,Pin.IN)
irq2 = Pin(13,Pin.IN)
irq1.irq(trigger=Pin.IRQ_FALLING,handler=irq_temperature)

def do_connect():
    import network
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print('没有连上网络')
        wlan.connect('jiubugaosuni','nicaibudaoadmin123')
        while not wlan.isconnected():
            tm.dat(0x40,3)
            tm.dat(0x40,2)
            tm.dat(0x40,1)
            tm.dat(0x40,0)

def rtc():
    rtc=RTC()
    time.sleep_ms(100)
    ntptime.settime() 
    return rtc.datetime()



def main():
    do_connect()
    while 1 :
        etime=rtc()
        tm.rtc_time(etime[4]+8,etime[5])
        time.sleep(10)

